<?php
require_once('Vega/Dao/Table.php');

class Mobitv_Admin_ClientDao extends Vega_Dao_Table {
    public function add($data) {
        $name = $this->_db->valueQuote($data['name']);
        $file_name = $this->_db->valueQuote($data['file_name']);
        $version = $this->_db->valueQuote($data['version']);
        $sql = "INSERT INTO device_client
                SET name=$name,
                    file_name=$file_name,
                    version=$version,
                    updated_time=now()
                ";
        return $this->_db->queryWrite($sql);
    }

    public function getList($page, $limit) {
        $offset     = $limit * ($page - 1);
        $_limit     = ' LIMIT ' . $limit;
        $_offset    = ' OFFSET ' . $offset;
        $sql = "SELECT * FROM device_client
                $_limit $_offset";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function countList() {
        $sql = "SELECT COUNT(id) as num FROM device_client";
        $this->_db->query($sql);
        $row = $this->_db->fetchRow();
        return $row['num'];
    }

    public function getDetail($id) {
        $sql = "SELECT * FROM device_client WHERE id=$id";
        $this->_db->query($sql);
        return $this->_db->fetchRow();
    }

    public function save($data, $id) {
        $name = $this->_db->valueQuote($data['name']);
        $file_name = $this->_db->valueQuote($data['file_name']);
        $version = $this->_db->valueQuote($data['version']);
        $sql = "UPDATE device_client
                SET name=$name,
                    file_name=$file_name,
                    version=$version,
                    updated_time=now()
                WHERE id=$id
                ";
        return $this->_db->queryWrite($sql);
    }

    public function deleteFromList($list) {
        $list = '(' . implode(',', $list) . ')';
        $sql = "DELETE FROM device_client WHERE id IN $list";
        return $this->_db->queryWrite($sql);
    }
}